package com.service.business.member.validator;

import com.service.business.base.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年10月06日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
public class VerifyCodeExistConstraint implements ConstraintValidator<VerifyCodeExistValidate, String> {


    @Autowired
    private ConfigService configService;

    private String codeType = "";

    @Override
    public void initialize(VerifyCodeExistValidate verifyCodeExistValidate) {
        this.codeType = verifyCodeExistValidate.value();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        log.debug("VerifyCodeExistConstraint.isValid:{}, type:{}", s, codeType);
        Boolean valid = configService.configValueIsValid(codeType);
        if (valid) {
            return StringUtils.isNotBlank(s);
        }
        return true;
    }


}
